package com.base.pig.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.base.pig.dto.AlarmDerviceStatus;
import com.base.pig.dto.ScDeviceDetailDto;
import com.base.pig.entity.ScAlarmManager;
import com.base.pig.entity.ScDevice;
import com.base.pig.mapper.ScAlarmManagerMapper;
import com.base.pig.mapper.ScDeviceMapper;
import com.base.pig.util.R;
import com.base.pig.service.ScAlarmManagerService;
import com.base.pig.service.SysDictItemService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @ProjectName
 * @Package com.pig4cloud.pig.lab.service.impl
 * @ClassName ScSourceManagerServiceImpl
 * @Description
 * @Company lab
 * @Author wangw
 * @Date 2020/4/22 0022 16:58
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Service
@AllArgsConstructor
public class ScAlarmManagerServiceImpl extends ServiceImpl<ScAlarmManagerMapper, ScAlarmManager> implements ScAlarmManagerService {

    @Autowired
    private SysDictItemService sysDictItemService;

    @Autowired
    private ScAlarmManagerMapper scAlarmManagerMapper;

    @Autowired
    private ScDeviceMapper scDeviceMapper;

    @Override
    public R getPageDeviceList(Map map) {
        QueryWrapper deviceWrapper = new QueryWrapper();
        // 全量查询
        if(StrUtil.isNotBlank(String.valueOf(map.get("deviceType")))){
            deviceWrapper.eq("device_type",String.valueOf(map.get("deviceType")));
        }
        if(StrUtil.isNotBlank(String.valueOf(map.get("deviceStatus")))){
            deviceWrapper.eq("device_status",String.valueOf(map.get("deviceStatus")));
        }
        if(StrUtil.isNotBlank(String.valueOf(map.get("authorizedUnitNumber")))){
            deviceWrapper.eq("authorized_unit_number",String.valueOf(map.get("authorizedUnitNumber")));
        }
        System.out.println(String.valueOf(map.get("startTime")).equals(""));
        System.out.println(String.valueOf(map.get("endTime")).equals(""));
        if(map.get("startTime") != null && !String.valueOf(map.get("startTime")).equals("")){
            deviceWrapper.ge("alarm_time",String.valueOf(map.get("startTime")));
        }
        if(map.get("endTime") != null && !String.valueOf(map.get("startTime")).equals("")){
            deviceWrapper.le("alarm_time",String.valueOf(map.get("endTime"))+" 23:59:59");
        }
//        if(StrUtil.isNotBlank(String.valueOf(map.get("principalId")))){
//            deviceWrapper.eq("principal_id",String.valueOf(map.get("principalId")));
//        }
        Page page = new Page();
        if(map.get("size") != null && !"".equals(map.get("size"))){
            page.setSize(Long.valueOf(map.get("size").toString()));
        }
        if(map.get("current") != null && !"".equals(map.get("current"))){
            page.setCurrent(Long.valueOf(map.get("current").toString()));
        }
        // 设置分页
        List<OrderItem> orders = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("alarm_time");
        orderItem.setAsc(false);
        orders.add(orderItem);
        page.setOrders(orders);
        IPage iPage = this.page(page,deviceWrapper);
        List<ScAlarmManager> list = page.getRecords();
        list.stream().map(scAlarmManager -> {
            String name = sysDictItemService.getDictOne(Long.valueOf(scAlarmManager.getDeviceType()),"device_type").getLabel();
            scAlarmManager.setDeviceTypeName(name);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("sn",scAlarmManager.getSn());
            ScDevice device = scDeviceMapper.selectOne(queryWrapper);
            scAlarmManager.setLatitude(device.getLatitude());
            scAlarmManager.setLongitude(device.getLongitude());
            return scAlarmManager;
        }).collect(Collectors.toList());
        return R.ok(iPage,"查询成功.");
    }

    @Override
    public Map alarmManagerDetail(ScDeviceDetailDto scDeviceDetailDto) {
        Map scAlarmManager = scAlarmManagerMapper.alarmManagerDetail(scDeviceDetailDto);
        return scAlarmManager;
    }

    /**
     * @Title updateAlarmManager
     * @Description 修改设备异常状态
     * @param orderCode :
     * @return com.pig4cloud.pig.lab.entity.ScAlarmManager
     * @author songhw
     * @date 2020/7/3 0003 14:18
     */
    public void updateStatusByOrderCode(String orderCode) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_code",orderCode);
        List<ScAlarmManager> list = scAlarmManagerMapper.selectList(wrapper);

        QueryWrapper deviceWrapper = new QueryWrapper();
        ScDevice scDevice = new ScDevice();

        ScAlarmManager scAlarmManager = new ScAlarmManager();
        for(int i=0;i<list.size();i++){
            scAlarmManager = list.get(i);
            scAlarmManager.setDeviceStatus(0);
            scAlarmManager.setOrderCode(orderCode);
            scAlarmManagerMapper.updateById(scAlarmManager);

            deviceWrapper.eq("sn",list.get(i).getSn());
            deviceWrapper.eq("manufacturer_number",list.get(i).getManufacturerNumber());
            scDevice.setDeviceStatus(0);
            scDevice.setOrderStatus(0);
            scDeviceMapper.update(scDevice,deviceWrapper);
        }
        updateAlarmManager(scAlarmManager);
    }
    public ScAlarmManager updateAlarmManager(ScAlarmManager scAlarmManager) {

        //调用websocket通知前端设备状态
        AlarmDerviceStatus alarmDerviceStatus = new AlarmDerviceStatus();
        alarmDerviceStatus.setManufacturerNumber(scAlarmManager.getManufacturerNumber());
        alarmDerviceStatus.setSn(scAlarmManager.getSn());
        alarmDerviceStatus.setDeviceStatus(scAlarmManager.getDeviceStatus());
        //AlarmSocketServer.sendAll(JSONUtil.toJsonStr(alarmDerviceStatus));
        return scAlarmManager;
    }

}
